#include<iostream>
#include<cstring>

using namespace::std;

void swap(char* str, int i, int j)
{
    char tmp = str[i];
    str[i] = str[j];
    str[j] = tmp;
}

/*
void CalcAllPermutation(char* perm, int from, int to)
{
    if (to <= 1)
    {
        return;
    }

    if (from == to)
    {
        for (int i = 0; i <= to; i++)
            cout << perm[i];
        cout << endl;
    }
    else
    {
        for (int j = from + 1; j <= to; j++)
        {
            swap(perm, j, from);
            CalcAllPermutation(perm, from + 1, to);
            swap(perm, j, from);
        }
    }
}
*/

bool CalcAllPermutation(char* perm, int num){
    int i;

    //①找到排列中最后（最右）一个升序的首位位置i，x = ai
    for (i = num - 2; (i >= 0) && (perm[i] >= perm[i + 1]); --i){
        ;
    }
    // 已经找到所有排列
    if (i < 0){
        return false;
    }

    int k;
    //②找到排列中第i位右边最后一个比ai 大的位置j，y = aj
    for (k = num - 1; (k > i) && (perm[k] <= perm[i]); --k){
        ;
    }

    //③交换x，y
    swap(perm[i], perm[k]);
    //④把第(i+ 1)位到最后的部分翻转
    reverse(perm + i + 1, perm + num);
    return true;
}

int main(int argc, char** argv)
{
    CalcAllPermutation(argv[1], strlen(argv[1]));

    return 0;
}
